package com.jia.videos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.videos.common.dto.MemberDto;
import com.jia.videos.common.exception.GlobalExceptionHandler;
import com.jia.videos.common.exception.JiaException;
import com.jia.videos.entity.Member;
import com.jia.videos.mapper.MemberMapper;
import com.jia.videos.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.videos.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2020-12-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public MemberDto logins(Member member) {
        String phone = member.getPhone();
        String password = MD5.encrypt(member.getPassword());
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone).eq("status",1);
        Integer integer = baseMapper.selectCount(wrapper);

        if (integer < 1) {
            MemberDto memberDto1 = new MemberDto();
            Member member1 = new Member();
            member1.setPhone(phone);
            member1.setPassword(password);
            member1.setAvatar("https://gulimall-jia.oss-cn-beijing.aliyuncs.com/avatar01.jpg");
            member1.setEmail("xxx@qq.com");
            member1.setNickname("小宝贝");
            member1.setUsername("user");
            member1.setGender(1);
            member.setLevel(0);
            baseMapper.insert(member1);
            BeanUtils.copyProperties(member1,memberDto1);
            return memberDto1;

        } else {
            MemberDto memberDto2 = new MemberDto();
            Member member2 = baseMapper.selectOne(wrapper);
            if (password.equals(member2.getPassword())) {
                memberDto2.setPhone(member2.getPhone());
                memberDto2.setAvatar(member2.getAvatar());
                memberDto2.setEmail(member2.getEmail());
                memberDto2.setGender(member2.getGender());
                memberDto2.setLevel(member2.getLevel());
                memberDto2.setNickname(member2.getNickname());
                memberDto2.setUsername(member2.getUsername());
                return memberDto2;
            }else{
                throw new JiaException(20002,"登录失败");
            }
        }

    }
}
